<?php
session_start();
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'user') {
    header("Location: ../login.php");
    exit();
}

require '../config.php';

$post_id = $_GET['id'];
$user_id = $_SESSION['user_id'];

// 获取帖子详情
$stmt = $conn->prepare("SELECT title, content FROM posts WHERE id = ? AND user_id = ?");
$stmt->bind_param("ii", $post_id, $user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($post_title, $post_content);
$stmt->fetch();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST['title'];
    $content = $_POST['content'];

    $stmt = $conn->prepare("UPDATE posts SET title = ?, content = ? WHERE id = ? AND user_id = ?");
    $stmt->bind_param("ssii", $title, $content, $post_id, $user_id);

    if ($stmt->execute()) {
        header("Location: index.php");
        exit();
    } else {
        echo "<p class='error'>编辑帖子失败，请重试</p>";
    }

    $stmt->close();
    $conn->close();
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>编辑帖子 - 论坛</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <style>
        body {
            background-image: url('https://images.unsplash.com/photo-1506748686214-e9df14d4d9d0');
            background-size: cover;
            background-position: center;
            color: #4c4948;
            font-family: 'Lato', Arial, sans-serif;
            margin: 0;
            display: flex;
            min-height: 100vh;
        }
        .overlay {
            background: rgba(255, 255, 255, 0.8);
            width: 100%;
            display: flex;
        }
        .sidebar {
            background-color: #2d3748;
            color: white;
            width: 250px;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            border-radius: 15px;
            margin: 20px;
        }
        .sidebar a {
            color: white;
            display: block;
            padding: 10px 20px;
            margin: 10px 0;
            background: #3b4252;
            border-radius: 25px;
            transition: background 0.3s;
        }
        .sidebar a:hover {
            background: #a0aec0;
        }
        .main-content {
            flex: 1;
            padding: 20px;
            display: flex;
            flex-direction: column;
            margin: 20px;
            border-radius: 15px;
            background: #fff;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }
        .bg-header {
            background-image: url('https://images.unsplash.com/photo-1542281286-9e0a16bb7366');
            background-size: cover;
            background-position: center;
            height: 200px;
            border-radius: 15px;
            position: relative;
            overflow: hidden;
        }
        .bg-header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.5);
            z-index: 0;
        }
        .bg-header h2 {
            position: relative;
            z-index: 1;
            color: white;
            text-align: center;
            line-height: 200px;
            font-size: 2rem;
            margin: 0;
        }
        .form-group {
            margin-bottom: 20px;
        }
        .form-group label {
            display: block;
            font-weight: bold;
            margin-bottom: 5px;
        }
        .form-group input,
        .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }
        button {
            background: #1a73e8;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            transition: background 0.3s;
        }
        button:hover {
            background: #0c4ea2;
        }
        a {
            color: #1a73e8;
            text-decoration: none;
            transition: color 0.3s;
        }
        a:hover {
            color: #0c4ea2;
        }
    </style>
</head>
<body>
    <div class="overlay">
        <div class="sidebar">
            <h2 class="text-3xl font-bold mb-8">编辑帖子</h2>
            <nav class="w-full">
                <a href="index.php" class="text-lg">我的主页</a>
                <a href="create_post.php" class="text-lg">发布新帖子</a>
                <a href="notifications.php" class="text-lg">通知</a>
                <a href="../logout.php" class="text-lg text-red-500 hover:text-red-700">退出登录</a>
            </nav>
        </div>
        <div class="main-content">
            <div class="bg-header">
                <h2>编辑帖子</h2>
            </div>
            <div class="card p-6">
                <form action="edit_post.php?id=<?php echo $post_id; ?>" method="POST">
                    <div class="form-group">
                        <label for="title">标题:</label>
                        <input type="text" id="title" name="title" value="<?php echo htmlspecialchars($post_title); ?>" required>
                    </div>
                    <div class="form-group">
                        <label for="content">内容:</label>
                        <textarea id="content" name="content" rows="6" required><?php echo htmlspecialchars($post_content); ?></textarea>
                    </div>
                    <button type="submit" class="mt-4">保存</button>
                </form>
                <a href="index.php" class="mt-4 inline-block">返回主页</a>
            </div>
        </div>
    </div>
</body>
</html>